<template>
  <el-dialog
  title="新增文章"
  :visible.sync="showDialog"
  width="50%"
  :before-close="close">
  <el-form  label-width="80px" :model="form" :rules="rules" ref="formData">
  <el-form-item label="文章标题" prop="title">
    <el-input v-model="form.title" placeholder="请输入文章标签"></el-input>
  </el-form-item>
  <el-form-item label="文章内容" prop="articleBody">
     <quill-editor ref="text"
                        v-model="form.articleBody"
                        class="myQuillEditor"
                        :options="editorOption" />
  </el-form-item>
  <el-form-item label="视频地址">
    <el-input v-model="form.type" placeholder="请输入视频地址"></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
  </span>
</el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'ArticlesAdd',
  data () {
    return {
      rules: {
        title: [
          { required: true, message: '请输入文章标签', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOption: {
        modules: {
          toolbar: [
            [
              'bold',
              'italic',
              'underline',
              'strike',
              { list: 'ordered' }, { list: 'bullet' },
              'blockquote',
              'code-block',
              'image',
              'link'
            ]
            // [{ header: 1 }, { header: 2 }]
            // [{ script: 'sub' }, { script: 'super' }]
          ]
        }
      }
    }
  },
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    form: {
      type: Object,
      required: true
    }
  },
  methods: {
    // 关闭弹窗
    close () {
      this.$emit('closeDialog')
    },
    confirm () {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          this.close()
          if (this.form.id) {
            await update(this.form).then(() => {
              this.$emit('newData', this.form)
            })
          } else {
            await add(this.form).then(() => {
              this.$emit('newData', this.form)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'></style>
